﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using tmAsk.Common;
using System.Data.SqlClient;

namespace tmAsk.Member
{
    public partial class reAsk : System.Web.UI.Page
    {
        tmAsk.Data.MakeHtml make = new tmAsk.Data.MakeHtml();
        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                function.CheckUserLogin();
                string aid = Request.QueryString["id"];

                DataTable dr = SQL.GetList("b_ask", "a_id='" + aid + "'", "*").Tables[0];

                if (dr.Rows.Count <= 0)
                {
                    Response.Write(function.alert_text(4, "该问题不存在！", ""));
                    return;
                }
                if ("0" != dr.Rows[0][0].ToString())
                {
                    asktitle.InnerText = dr.Rows[0]["a_title"].ToString();
                    askcon.InnerHtml = dr.Rows[0]["a_content"].ToString();
                    username.InnerText = GetUserName(dr.Rows[0]["u_id"].ToString());
                    usertime.InnerText = dr.Rows[0]["a_time"].ToString();
                }
            }

        }

        public string GetUserName(object uid)
        {
            string username = "";
            DataTable dt = SQL.GetList("select u_nickName from b_user where u_id=" + uid).Tables[0];
            if (dt.Rows.Count > 0)
            {
                username = dt.Rows[0][0].ToString();
            }
            return username;
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            if (!function.CheckNull(reask.Value))
            {
                Response.Write(function.alert_text(4, "请输入回复内容！", ""));
                return;
            }
            else
            {
                string sqluser = "select u_id from b_user where u_id='" + function.Getcookies() + "'";
                DataTable dr = SQL.GetList(sqluser).Tables[0];
                if (dr.Rows.Count > 0)
                {
                    string sql = "select u_id,a_title,a_id from B_ask where a_state = 0 and a_id =@a_id";
                    SqlParameter[] eparameters = new SqlParameter[]{
                        new SqlParameter("@a_id", SqlDbType.Char),
                    };
                    eparameters[0].Value = Request.QueryString["id"];

                    DataTable rs = SQL.GetList(sql, eparameters).Tables[0];
                    if (rs.Rows.Count > 0)
                    {
                        if (dr.Rows[0]["u_id"].ToString() == rs.Rows[0]["u_id"].ToString())
                        {
                            Response.Write(function.alert_text(4, "自己发布的问题，不允许回答！", ""));
                            return;
                        }
                        //回复内容，给作者发email
                        (new alldata()).sendemail("1", rs.Rows[0]["u_id"].ToString(), "您发布的问题“" + function.CutString(rs.Rows[0]["a_title"].ToString(), "15") + "”有人回答了，<a href=" + (new function()).Getlink("ask", rs.Rows[0]["a_id"].ToString()) + " target=_blank>请查看</a>");
                        DataTable rrs = SQL.GetList("select r_id from b_review where a_id=" + Request.QueryString["id"] + " and r_userid=" + dr.Rows[0]["u_id"].ToString()).Tables[0];
                        //增加回复记录
                        string rsql = "insert into B_review(r_content,r_time,a_id,r_isGood,r_agreeNumber,r_unagreeNumber,r_userid,r_hiddenuser) values(";
                        rsql += "@r_content,@r_time,@a_id,@r_isGood,@r_agreeNumber,@r_unagreeNumber,@r_userid,@r_hiddenuser)";
                        SqlParameter[] rparameters = new SqlParameter[]{
                        new SqlParameter("@r_content", function.HTMLEncode(reask.Value)),
                        new SqlParameter("@r_time", DateTime.Now.ToString()),
                        new SqlParameter("@a_id", Request.QueryString["id"]),
                        new SqlParameter("@r_isGood", "0"),
                        new SqlParameter("@r_agreeNumber", "0"),
                        new SqlParameter("@r_unagreeNumber", "0"),
                        new SqlParameter("@r_userid", dr.Rows[0]["u_id"].ToString()),
                        new SqlParameter("@r_hiddenuser", rs.Rows[0]["u_id"].ToString()),
                    };

                        int id = SQL.Add(rsql, rparameters);

                        //更新回复数量

                        SQL.ExecuteSql("update b_ask set a_reviewNumber=a_reviewNumber+1 where a_id=" + Request.QueryString["id"]);
                        SQL.ExecuteSql("update b_user set u_reviewNumber=u_reviewNumber+1 where u_id=" + dr.Rows[0]["u_id"].ToString());
                        ////////////////////////
                        Response.Write(function.alert_text(2, "回复成功！", "ReQuestion.aspx?id=" + Request.QueryString["id"]));
                    }
                    else
                    {
                        Response.Write(function.alert_text(4, "该问题不能回答，已经关闭！", ""));
                        return;
                    }
                }
            }
        }
    }
}